// 豆瓣全选/反选辅助工具
// version 0.4 BETA!
// 2011-11-06
// Copyright (c) 2009, icyleaf
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
//
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.  To install it, you need
// Greasemonkey 0.3 or later: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts,
// select "豆瓣全选/反选辅助工具", and click Uninstall.
//
// --------------------------------------------------------------------
//
// ==UserScript==
// @name          豆瓣全选/反选辅助工具
// @namespace     http://icyleaf.com
// @description   允许下述 URL 地址可以辅助用户全选和全否的辅助操作
// @include        http://www.douban.com/group/*/invite*
// @include        http://www.douban.com/online/*/invite*
// @include        http://www.douban.com/event/*/invite* 
// @include        http://www.douban.com/doumail/*
// ==/UserScript==

// Add jQuery
var GM_JQ = document.createElement('script');
GM_JQ.src = 'http://jquery.com/src/jquery-latest.js';
GM_JQ.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(GM_JQ);

GM_wait();

// All your GM code must be inside this function
function letsJQuery() {
	var url = window.location;
	var reg_match = /doumail/i;

	var title = '';
	var keyword = '';
	if (reg_match.test(url)) {
		title = '豆邮';
		keyword = 'doumail';
	} else {
		title = '用户';
		keyword = 'people';
	}

	var block = '<div><h2>辅助工具   · · · · · ·  </h2>' +
			'<p class="pl2">' +
				'> <a href="javascript:void(0);" id="' + keyword + '_select_all">' + title + '全选</a>' +
			'</p>' +
			'<p class="pl2">' +
				'> <a href="javascript:void(0);" id="' + keyword + '_unselect_all">' + title + '全不选</a>' +
			'</p>' +
		'</div>';

	// Add Action Button
	$('.aside').append(block);

	// Add user select all function
	$('#people_select_all').bind('click', function() {
		var total = 0;
		var selected_count = 0;
		$('.obu').each(function(i) {
			var status = $(':checkbox', this).attr('checked');
			if ( ! status) {
				$(':checkbox', this).attr('checked', true);
				selected_count++;
			}
			total = i;
		});
		total++;

		alert('从  ['+total+'] 位用户中选中了 ['+selected_count+'] 位');
	});
	
	// Add user unselect all function
	$('#people_unselect_all').bind('click', function() {
		var total = 0;
		var selected_count = 0;
		var selected_flag = false;

		$('.obu').each(function(i){
			var status = $(':checkbox', this).attr('checked');
			if ( status ) {
				selected_flag = true;
				$(':checkbox', this).attr('checked', false);
				selected_count++;
			}
			total = i;
		});
		
		if (selected_flag) {
			total++;
			alert('从 ['+total+'] 位用户中取消了 ['+selected_count+'] 位');
		} else {
			alert('当前没有选中的用户');
		}
	});


	// Add doumail select all function
	$('#doumail_select_all').bind('click', function() {
		var total = 0;
		var selected_count = 0;
		$('table.olt tr td[align=center]').each(function(i) {
			var status = $(':checkbox', this).attr('checked');
			if ( ! status) {
				$(':checkbox', this).attr('checked', true);
				selected_count++;
			}
			total = i;
		});
		total++;

		alert('从  ['+total+'] 封豆邮中选中了 ['+selected_count+'] 封');
	});

	// Add doumail unselect all function
	$('#doumail_unselect_all').bind('click', function() {
		var total = 0;
		var selected_count = 0;
		var selected_flag = false;

		$('.table.olt tr td[align=center]').each(function(i){
			var status = $(':checkbox', this).attr('checked');
			if (status) {
				selected_flag = true;
				$(':checkbox', this).attr('checked', false);
				selected_count++;
			}
			total = i;
		});
		
		if (selected_flag) {
			total++;
			alert('从 ['+total+'] 封豆邮中取消了 ['+selected_count+'] 封');
		} else {
			alert('当前没有选中的豆邮');
		}
	});
}

// Check if jQuery's loaded
function GM_wait() {
	if(typeof unsafeWindow.jQuery == 'undefined') { 
		window.setTimeout(GM_wait, 100); 
	} else { 
		$ = unsafeWindow.jQuery; 
		letsJQuery(); 
	}
}


